package com.mjl.shops.activity;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import com.mjl.shops.R;
import com.mjl.shops.base.BaseActivity;
import com.mjl.shops.fragment.AFOrderFragment;
import com.mjl.shops.fragment.AllOrderFragment;
import com.mjl.shops.fragment.WFHOrderFragment;
import com.mjl.shops.fragment.WFKOrderFragment;
import com.mjl.shops.fragment.WSHOrderFragment;
import com.mjl.shops.utils.CommUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderStateActivity extends BaseActivity {

    private static final int ALL = 0;
    private static final int WFK = 1;
    private static final int WFH = 2;
    private static final int WSH = 3;
    private static final int AF = 4;

    private int mNextIndex, mCurrentIndex;

    private TextView[] textViews;

    private TextView[] xianViews;

    private Fragment[] fragments;

    private Fragment all, wfk, wfh, wsh, af;

    @BindView(R.id.all_tv)
    TextView all_tv;
    @BindView(R.id.all_xian)
    TextView all_xian;

    @BindView(R.id.wait_fk_tv)
    TextView wait_fk_tv;
    @BindView(R.id.wait_fk_xian)
    TextView wait_fk_xian;

    @BindView(R.id.wait_fh_tv)
    TextView wait_fh_tv;
    @BindView(R.id.wait_fh_xian)
    TextView wait_fh_xian;

    @BindView(R.id.wait_sh_tv)
    TextView wait_sh_tv;
    @BindView(R.id.wait_sh_xian)
    TextView wait_sh_xian;

    @BindView(R.id.wait_finsh_tv)
    TextView wait_finsh_tv;
    @BindView(R.id.wait_finsh_xian)
    TextView wait_finsh_xian;

    @Override
    protected void setContentViewBefore() {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    @Override
    protected int getViews() {
        return R.layout.activity_order_state;
    }

    @Override
    protected void setViews() {
        ButterKnife.bind(this);
        all = new AllOrderFragment();
        wfk = new WFKOrderFragment();
        wfh = new WFHOrderFragment();
        wsh = new WSHOrderFragment();
        af = new AFOrderFragment();
        fragments = new Fragment[]{all, wfk, wfh, wsh, af};
        textViews = new TextView[]{all_tv, wait_fk_tv, wait_fh_tv, wait_sh_tv, wait_finsh_tv};
        xianViews = new TextView[]{all_xian, wait_fk_xian, wait_fh_xian, wait_sh_xian, wait_finsh_xian};
    }

    @Override
    protected void setDate() {
        mCurrentIndex = getIntent().getIntExtra("INDEX", 0);
        setDefFragment();
    }

    @Override
    protected String getStateColor() {
        return CommUtils.color_05000000;
    }

    @Override
    protected int getStetaBackgroundStyle() {
        return 1;
    }

    //设置默认fragment
    private void setDefFragment() {
        setSelect(mCurrentIndex);
        Fragment fs = fragments[mCurrentIndex];
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.order_frame, fs, fs.getClass().getName());
        transaction.commitAllowingStateLoss();//
    }

    @OnClick({R.id.all_ll, R.id.wait_fk_ll, R.id.wait_fh_ll, R.id.wait_sh_ll, R.id.wait_finsh_ll})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.all_ll:
                mNextIndex = 0;
                break;
            case R.id.wait_fk_ll:
                mNextIndex = 1;
                break;
            case R.id.wait_fh_ll:
                mNextIndex = 2;
                break;
            case R.id.wait_sh_ll:
                mNextIndex = 3;
                break;
            case R.id.wait_finsh_ll:
                mNextIndex = 4;
                break;
            default:
                break;
        }
        setFragment();
    }

    private void setFragment() {
        if (mNextIndex == mCurrentIndex){
            return;
        }
        setSelect(mNextIndex);
        setNotSelect(mCurrentIndex);
        changeFragment();
        mCurrentIndex = mNextIndex;
    }

    //切换fragment
    private void changeFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (mNextIndex) {
            case ALL://首页
                //判断当前的Fragment是否为空，不为空则隐藏
                if (null != fragments[mCurrentIndex]) {
                    transaction.hide(fragments[mCurrentIndex]);
                }
                //先根据Tag从FragmentTransaction事物获取之前添加的Fragment
                Fragment fragment1;
                if (fragments[mNextIndex] == null) {
                    fragments[mNextIndex] = all;
                    fragment1 = all;
                } else {
                    fragment1 = getSupportFragmentManager().findFragmentByTag(fragments[mNextIndex].getClass().getName());
                    if (null == fragment1) {
                        //如fragment为空，则之前未添加此Fragment。便从集合中取出
                        if (fragments[mNextIndex] == null) {
                            fragments[mNextIndex] = all;
                        }
                        fragment1 = all;
                    }
                }
                //判断此Fragment是否已经添加到FragmentTransaction事物中
                if (!fragment1.isAdded()) {
                    transaction.add(R.id.order_frame, fragment1, fragment1.getClass().getName());
                } else {
                    transaction.show(fragment1);
                }
                transaction.commit();
                break;
            case WFK:
                //判断当前的Fragment是否为空，不为空则隐藏
                if (null != fragments[mCurrentIndex]) {
                    transaction.hide(fragments[mCurrentIndex]);
                }
                Fragment fragment2;
                if (fragments[mNextIndex] == null) {
                    fragments[mNextIndex] = wfk;
                    fragment2 = wfk;
                } else {
                    //先根据Tag从FragmentTransaction事物获取之前添加的Fragment
                    fragment2 = getSupportFragmentManager().findFragmentByTag(fragments[mNextIndex].getClass().getName());
                    if (null == fragment2) {
                        //如fragment为空，则之前未添加此Fragment。便从集合中取出
                        if (fragments[mNextIndex] == null) {
                            fragments[mNextIndex] = wfk;
                        }
                        fragment2 = wfk;
                    }
                }
                //判断此Fragment是否已经添加到FragmentTransaction事物中
                if (!fragment2.isAdded()) {
                    transaction.add(R.id.order_frame, fragment2, fragment2.getClass().getName());
                } else {
                    transaction.show(fragment2);
                }
                transaction.commit();
                break;
            case WFH:
                //判断当前的Fragment是否为空，不为空则隐藏
                if (null != fragments[mCurrentIndex]) {
                    transaction.hide(fragments[mCurrentIndex]);
                }
                Fragment fragment3;
                if (fragments[mNextIndex] == null) {
                    fragments[mNextIndex] = wfh;
                    fragment3 = wfh;
                } else {
                    //先根据Tag从FragmentTransaction事物获取之前添加的Fragment
                    fragment3 = getSupportFragmentManager().findFragmentByTag(fragments[mNextIndex].getClass().getName());
                    if (null == fragment3) {
                        //如fragment为空，则之前未添加此Fragment。便从集合中取出
                        if (fragments[mNextIndex] == null) {
                            fragments[mNextIndex] = wfh;
                        }
                        fragment3 = wfh;
                    }
                }
                //判断此Fragment是否已经添加到FragmentTransaction事物中
                if (!fragment3.isAdded()) {
                    transaction.add(R.id.order_frame, fragment3, fragment3.getClass().getName());
                } else {
                    transaction.show(fragment3);
                }
                transaction.commit();
                break;
            case WSH:
                //判断当前的Fragment是否为空，不为空则隐藏
                if (null != fragments[mCurrentIndex]) {
                    transaction.hide(fragments[mCurrentIndex]);
                }
                Fragment fragment4;
                if (fragments[mNextIndex] == null) {
                    fragments[mNextIndex] = wsh;
                    fragment4 = wsh;
                } else {
                    //先根据Tag从FragmentTransaction事物获取之前添加的Fragment
                    fragment4 = getSupportFragmentManager().findFragmentByTag(fragments[mNextIndex].getClass().getName());
                    if (null == fragment4) {
                        //如fragment为空，则之前未添加此Fragment。便从集合中取出
                        if (fragments[mNextIndex] == null) {
                            fragments[mNextIndex] = wsh;
                        }
                        fragment4 = wsh;
                    }
                }
                //判断此Fragment是否已经添加到FragmentTransaction事物中
                if (!fragment4.isAdded()) {
                    transaction.add(R.id.order_frame, fragment4, fragment4.getClass().getName());
                } else {
                    transaction.show(fragment4);
                }
                transaction.commit();
                break;
            case AF:
                //判断当前的Fragment是否为空，不为空则隐藏
                if (null != fragments[mCurrentIndex]) {
                    transaction.hide(fragments[mCurrentIndex]);
                }
                Fragment fragment5;
                if (fragments[mNextIndex] == null) {
                    fragments[mNextIndex] = af;
                    fragment5 = af;
                } else {
                    //先根据Tag从FragmentTransaction事物获取之前添加的Fragment
                    fragment5 = getSupportFragmentManager().findFragmentByTag(fragments[mNextIndex].getClass().getName());
                    if (null == fragment5) {
                        //如fragment为空，则之前未添加此Fragment。便从集合中取出
                        if (fragments[mNextIndex] == null) {
                            fragments[mNextIndex] = af;
                        }
                        fragment5 = af;
                    }
                }
                //判断此Fragment是否已经添加到FragmentTransaction事物中
                if (!fragment5.isAdded()) {
                    transaction.add(R.id.order_frame, fragment5, fragment5.getClass().getName());
                } else {
                    transaction.show(fragment5);
                }
                transaction.commit();
                break;
        }
    }

    //设置选中文字颜色和下划线颜色
    private void setSelect(int i) {
        textViews[i].setTextColor(Color.parseColor("#1BBF80"));
        xianViews[i].setVisibility(View.VISIBLE);
    }

    //设置未选中文字颜色和下划线颜色
    private void setNotSelect(int i) {
        textViews[i].setTextColor(Color.parseColor("#999999"));
        xianViews[i].setVisibility(View.INVISIBLE);
    }

    public static void StartIntent(Context contexts, int index) {
        Intent intent = new Intent(contexts, OrderStateActivity.class);
        intent.putExtra("INDEX", index);
        contexts.startActivity(intent);
    }
}
